/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Model;

import java.awt.Color;
import java.util.*;

/**
 *
 * @author snowangelic
 */
public class Action {
    ArrayList<AttributeValue> attributeList;
    ArrayList<Cluster> clusterList;
    Point point;

    Action(){
        attributeList=new  ArrayList<AttributeValue>();
        clusterList=new ArrayList<Cluster>();
        point=new Point();
    }

    Action(ArrayList<AttributeValue> attributes) {

        point=new Point();
        
        // Parcours de la liste
        Iterator it=attributes.iterator();
        while(it.hasNext()){
           AttributeValue attr=(AttributeValue) it.next();
           attr.setAction(this);
        }
        attributeList=attributes;
        clusterList=new ArrayList<Cluster>();

    }

    void setValue(Attribute attribute,Object value){
        Iterator it=attributeList.iterator();
            while(it.hasNext()){
                AttributeValue currentAttrValue=(AttributeValue) it.next();
                if(currentAttrValue.getAttribute().equals(attribute)){
                    attributeList.remove(currentAttrValue);
                }
            }
            AttributeValue<Object> attrVal = new AttributeValue<Object>(this,attribute,value);
            attributeList.add(attrVal);
    }

    /**
     *
     * @param attr
     * @return
     */
    public Object getValue(Attribute attr){
        Object ret=null;
        // Parcours de la liste à la recherche d'un attributeValue qui point vers attr
        Iterator it=attributeList.iterator();
        while(it.hasNext()){
           AttributeValue attrVal=(AttributeValue) it.next();
           if(attrVal.getAttribute().equals(attr)){ // Si on a trouvé
               ret=attrVal.getValue();
           }
        }
        return ret;
    }

    Cluster getCluster(Kmeans kmeans) {
        Iterator it=clusterList.iterator();
        while(it.hasNext()){
            Cluster currentCluster=(Cluster) it.next();
            if(currentCluster.getKmeans().equals(kmeans)){
                return currentCluster;
            }
        }
        return null;
    }

    void setCluster(Cluster cluster) {
        for(int i=0;i<clusterList.size();i++){
            Cluster currentCluster=clusterList.get(i);
            if(currentCluster.getKmeans().equals(cluster.getKmeans())){
                clusterList.remove(i);
                i--;
            }
        }
        clusterList.add(cluster);
    }

    public void setCoords(float[] coords){
        point.setCoords(coords[0], coords[1], coords[2]);
    }

    float[] getCoords(){
        return point.getCoords();
    }

    Color getColor(Kmeans kmeans){
        Cluster cluster;
        cluster=this.getCluster(kmeans);
        if (cluster!=null) return cluster.getColor();
        else{
            Color color=new Color(1,1,1,0);
            return color;
        }
    }

}
